<?php

namespace Frieza\Modules\Device\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\service\DeviceBlackWhite;
use Doraemon\model\DeviceBlackWhite as mDeviceBlackWhite;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

class DeviceBlackController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    public function index(Request $request, Content $content)
    {
        $params = $request->input();
        $pageSize = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);
        $search = [];
        if (!empty($params['device_id'])) {
            $search['device_id'] = $params['device_id'];
        }
        $list = DeviceBlackWhite::getDeviceBlackList($page, $pageSize, $search);

        $gridModel = new pGridDataModel($list['list'], $list['total'], [
            'perPage' => $pageSize
        ]);

        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
//                $grid->disablePagination();
//                $grid->disableFilter();
                $grid->disableRowSelector();

                $grid->filter(function($filter){
                    $filter->disableIdFilter();
                    $filter->equal('device_id','设备id');
                });
                $grid->column('id');
                $grid->column('device_id', '设备id');
                $grid->column('data_type', '类型')->display(function () {
                    return mDeviceBlackWhite::DEVICE_TYPE[$this->data_type];
                });
                $grid->column('add_time', '添加时间')->display(function () {
                    return date("Y-m-d H:i:s", $this->add_time);
                });

                $grid->column('edit_time', '修改时间')->display(function () {
                    if ($this->edit_time) {
                        return date("Y-m-d H:i:s", $this->edit_time);
                    } else {
                        return '--';
                    }

                });

                $grid->column('data_status', '类型')->display(function () {
                    return mDeviceBlackWhite::DATA_STATUS[$this->data_status];
                });

                $grid->column('admin_id', '操作人')->display(function(){
                    if(!$this->admin_id) {
                        return '暂无';
                    }else{
                        return Admin::user()->find($this->admin_id)->name;
                    }
                });
            });

        return $content
            ->header('设备黑/白名单')
            ->description(' ')
            ->breadcrumb(['text' => '设备黑/白名单'])
            ->row($gridList);
    }


    public function del($id)
    {
        $data = mDeviceBlackWhite::getById($id);
        if($data) {
            $data['data_status'] = mDeviceBlackWhite::STATUS_DELETE;
            $data['edit_time'] = time();

            $rs = mDeviceBlackWhite::updateById($id, $data);
            if($rs) {
                return response()->json([
                    'status' => true,
                    'message' => "操作成功",
                ]);
            } else {
                return response()->json([
                    'status' => false,
                    'message' => "操作失败",
                ]);
            }
        } else {
            return response()->json([
                'status' => false,
                'message' => "操作失败",
            ]);
        }
    }

    public function add(Request $request, Content $content)
    {

        $request = $request->all();
        return $content
            ->header('设备黑/白名单')
            ->breadcrumb(
                ['text' => '添加设备黑/白名单']
            )
            ->body(self::editAction($request, $id = 0));

    }

    public function edit(Request $request, Content $content, $id)
    {

        $request = $request->all();
        return $content
            ->header('设备黑/白名单')
            ->breadcrumb(
                ['text' => '修改设备黑/白名单']
            )
            ->body(self::editAction($request, $id));

    }

    public function editAction($request, $id = 0)
    {
        $data = [];
        if($id != 0) {
            //修改
            $data = mDeviceBlackWhite::getById($id);
            $form = new Form($data);
            $form->hidden('id');
        } else {
            //添加
            $form = new Form($data);
        }
        $form->hidden('id');
        $form->text('device_id', '设备id')->setWidth(4, 2)->required('不能为空')->help('请输入设备id');
        $form->select('data_type', '类型')->setWidth(4, 2)->options(mDeviceBlackWhite::DEVICE_TYPE);
        $form->action('/admin/device/index/save');

        return $form->render();
    }

    public function editSubmit(Request $request, Content $content)
    {
        $request = $request->all();
        $rs = false;

        $data['device_id'] = $request['device_id'];
        $data['data_type'] = $request['data_type'];
        $data['admin_id'] = Admin::user()->id;


        if(isset($request['id']) && $request['id']) {
            $data['edit_time'] = time();
            //修改
            $rs = mDeviceBlackWhite::updateById($request['id'], $data);
        } else {
            $data['add_time'] = time();
            //添加
            $info = mDeviceBlackWhite::selectByWhere(['device_id' => $data['device_id'], 'data_status' => mDeviceBlackWhite::STATUS_NORMAL]);
            if (empty($info)) {
                $rs = mDeviceBlackWhite::add($data);
            }

        }
        if($rs) {
            $success = new MessageBag([
                'title' => '提交成功'
            ]);
            return redirect('/admin/device/index')->with(compact('success'));
        } else {
            $error = new MessageBag([
                'title' => '提交失败'
            ]);
            return redirect('/admin/device/index')->with(compact('error'));
        }


    }

}